package com.tengju.user.infrastructure.repository;

import com.tengju.user.domain.model.sign.StarConfIndicator;
import com.tengju.user.domain.model.sign.StarIndicatorRepository;
import com.tengju.user.domain.model.sign.StarIndicatorStat;
import com.tengju.user.domain.model.sign.StarsIndicatorConfCodeEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.CastUtil;
import com.tengju.user.domain.shared.DateUtil;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.CommonConfigDataDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.StarIndicatorStatDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.CommonConfigDataDOCustomMapper;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.DwsStarIndicatorStatMapper;
import com.tengju.user.infrastructure.repository.convert.StarSignConvert;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class StarIndicatorRepositoryImpl implements StarIndicatorRepository {

    @Resource
    CommonConfigDataDOCustomMapper configDataDOCustomMapper;

    @Resource
    DwsStarIndicatorStatMapper starIndicatorStatMapper;

    @Override
    public List<StarConfIndicator> listConfIndicator(Integer starLevel) {
        StarsIndicatorConfCodeEnum confCodeEnum = StarsIndicatorConfCodeEnum.getByStarLevel(starLevel);
        if (confCodeEnum == null) {
            return Collections.emptyList();
        }
        List<CommonConfigDataDO> configDataDOList = configDataDOCustomMapper.listByGroupCode(confCodeEnum.getCode());
        return configDataDOList.stream()
                .map(StarSignConvert::configDOToStarConfIndicator)
                .collect(Collectors.toList());
    }

    @Override
    public List<StarIndicatorStat> listStarIndicatorStat(UserIdCode idCode) {
        String dateStr = DateUtil.parseDateToString(DateUtil.DATE_FORMAT, new Date());
        int day = CastUtil.castInt(dateStr);
        List<StarIndicatorStatDO> starIndicatorStatDOS = starIndicatorStatMapper.batchQueryByIdCodeAndDate(idCode, day);
        return starIndicatorStatDOS.stream()
                .map(StarSignConvert::starIndicatorToEntity)
                .collect(Collectors.toList());
    }
}
